﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Threading;
using System.Globalization;
using Nivdal.AuxCode;

namespace Nivdal
{
    public partial class ApproveUsers : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!HttpContext.Current.User.Identity.IsAuthenticated)
                Response.Redirect("~/Default.aspx");
            else
            {

                OnlineUser ou = (OnlineUser)Session["User"];
                UserIdHiddenField.Value = ou.UserId;

                if (ou.UserAccessLevel != ("4") && ou.UserAccessLevel != ("5"))
                {
                    Response.Redirect("~/Default.aspx");
                }
            }
            UpdateNamesPanel.Visible = (NameApprovalsGridview.Rows.Count > 0);
            this.Title = Resources.Resource.Default_PageTitle + ", " + Resources.Resource.ApproveUsers_PageTitle;
        }

        //protected string ApproveUser(string UserId)
        //{
        //    GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
        //    string Answer = GND.ReadNivdalStringData("UserId", "Nivdal_UserApprovalApproveUser", UserId);
        //    ApprovalUsersGridView.DataBind();

        //    //Response.Write(UserId);
        //    string returnValue = Resources.Resource.Ok;
        //    string conn = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        //    System.Data.SqlClient.SqlConnection
        //    strConnection = new System.Data.SqlClient.SqlConnection(conn);
        //    string StrSQL = "Nivdal_UserApprovalApproveUser";
        //    System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(StrSQL, strConnection);
        //    cmd.CommandType = System.Data.CommandType.StoredProcedure;
        //    cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@UserId", UserId));

        //    try
        //    {
        //        strConnection.Open(); 
        //        cmd.ExecuteReader();

        //    }
        //    catch (Exception SQLDBException)
        //    {
        //        returnValue = SQLDBException.ToString();
        //        return returnValue;
        //        //Response.Write(SQLDBException.ToString());

        //    }

        //    finally
        //    {
        //        strConnection.Close();
        //    }


        //    return returnValue;
        //}


        //protected string DeclineUser(string UserId)
        //{
        //    //Response.Write(UserId);
        //    string returnValue = Resources.Resource.Ok;
        //    string conn = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        //    System.Data.SqlClient.SqlConnection
        //    strConnection = new System.Data.SqlClient.SqlConnection(conn);
        //    string StrSQL = "Nivdal_UserApprovalDeclineUser";
        //    System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(StrSQL, strConnection);
        //    cmd.CommandType = System.Data.CommandType.StoredProcedure;
        //    cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@UserId", UserId));
        //    strConnection.Open();
        //    try
        //    {
        //        cmd.ExecuteReader();

        //    }
        //    catch (Exception SQLDBException)
        //    {
        //        returnValue = SQLDBException.ToString();
        //        return returnValue;
        //        //Response.Write(SQLDBException.ToString());

        //    }

        //    finally
        //    {
        //        strConnection.Close();
        //    }


        //    return returnValue;
        //}


        //protected string MoveToAdmin(string UserId)
        //{
        //    Response.Write(UserId);
        //    string returnValue = Resources.Resource.Ok;
        //    string conn = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        //    System.Data.SqlClient.SqlConnection
        //    strConnection = new System.Data.SqlClient.SqlConnection(conn);
        //    string StrSQL = "Nivdal_UserApprovalMoveToAdmin";
        //    System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(StrSQL, strConnection);
        //    cmd.CommandType = System.Data.CommandType.StoredProcedure;
        //    cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@UserId", UserId));
        //    strConnection.Open();
        //    try
        //    {
        //        cmd.ExecuteReader();

        //    }
        //    catch (Exception SQLDBException)
        //    {
        //        returnValue = SQLDBException.ToString();
        //        return returnValue;
        //        //Response.Write(SQLDBException.ToString());

        //    }

        //    finally
        //    {
        //        strConnection.Close();
        //    }


        //    return returnValue;
        //}


        protected void UpdateNewUser(Object sender, GridViewCommandEventArgs e)
        {
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            string Answer = "";
            if (e.CommandName == "ApproveUser")
            {
                Answer = GND.ReadNivdalStringData("UserId", "Nivdal_UserApprovalApproveUser", e.CommandArgument);
                GND.SendPendingMails();
            }
            if (e.CommandName == "DeclineUser")
            {
                Answer = GND.ReadNivdalStringData("UserId", "Nivdal_UserApprovalDeclineUser", e.CommandArgument);
            }

            if (e.CommandName == "MoveToAdmin")
            {
                Answer = GND.ReadNivdalStringData("UserId", "Nivdal_UserApprovalMoveToAdmin", e.CommandArgument);
            }

            AccessLevelLabel.Text = Answer;
            ApprovalUsersGridView.DataBind();

        }

        //protected void ApprovalUsersGridView_OnRowCreated(object sender, GridViewRowEventArgs e)
        //{
        //    e.Row.Cells[0].Visible = false;

        //}

        protected string ApproveUserLinkButtonOnClientClick()
        {
            return ("return confirm('" + Resources.Resource.ApproveUser_AreYouSure + "')");
        }

        protected string DeclineUserLinkButtonOnClientClick()
        {
            return ("return confirm('" + Resources.Resource.DeclineUser_AreYouSure + "')");
        }

        protected string MoveToAdminUserLinkButtonOnClientClick()
        {
            return ("return confirm('" + Resources.Resource.ApproveUsers_MoveToAdminUserLinkButton + "')");
        }


        protected override void InitializeCulture()
        {
            OnlineUser ou = (OnlineUser)Session["User"];
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(ou.UICulture);
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ou.Culture);
        }

        protected void ApproveAllNames(object sender, EventArgs e)
        {
            foreach (GridViewRow row in NameApprovalsGridview.Rows)
            {
                if ((row.FindControl("ApproveCheckBox") as CheckBox).Checked)
                {
                    string UserId = (row.FindControl("UserIdHiddenField") as HiddenField).Value;
                    GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
                    GND.ExecNivdalSQL("UserId", "Nivdal_ApproveTeamName", UserId);
                }
            }

            NameApprovalsGridview.DataBind();
        }


    }
}